package com.paypalpay.controller.pay;

import com.paypalpay.entity.common.UserboundPaymentOrder;
import com.paypalpay.pojo.Constant;
import com.paypalpay.pojo.request.RequestBody.PayNotifyRequestBody;
import com.paypalpay.pojo.request.RequestHeader;
import com.paypalpay.pojo.request.subRequest.PayNotifyRequest;
import com.paypalpay.pojo.response.subResponse.PayNotifyResponse;
import com.paypalpay.service.PartnerService;
import com.paypalpay.service.UserboundPaymentOrderService;
import com.paypalpay.turntointerfaceservice.utils.TurntoInterfaceUtils;
import com.paypalpay.util.JaxbUtil;
import com.paypalpay.util.wx.HttpClientTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Demo 正常流程支付结果通知接口（跟北京后台交互的）
 *
 * @author lm
 * @date 2018/09/10
 */
@RestController
public class NotificationPaymentResultsController {

    @Autowired
    UserboundPaymentOrderService userboundPaymentOrderService;

    @Autowired
    PartnerService partnerService;

    @RequestMapping(value = "/payNotify")
    @CrossOrigin
    @ResponseBody
    public void payNotify(@RequestParam(value = "orderId")String orderId, @RequestParam(value = "deviceId")String deviceId,@RequestParam(value = "deviceType") String deviceType,@RequestParam(value = "payType",required = false) String payType, @RequestParam(value = "payAmount")String payAmount,@RequestParam(value = "partnerCode")String partnerCode) {
        try {
            String secret = partnerService.findSecretByPartnerCode(partnerCode);
            System.out.println("进到payNotify支付结果通知开始！");
            //请求body参数处理
            PayNotifyRequestBody body = new PayNotifyRequestBody();
            body.setOrderId(orderId);
            body.setDeviceId(deviceId);
            body.setDeviceType(deviceType);
            body.setPayType(payType);
            body.setPayAmount(payAmount);

            //参数拼接
            PayNotifyRequest request = new PayNotifyRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_payNotify, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(PayNotifyRequest.class).toXml(request, "UTF-8");
            System.out.println("请求报文：" + sRequest);
            //String url = "http://localhost:80/TestService/test/payNotify";
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            System.out.println("响应报文：" + result);
            PayNotifyResponse responseObj = new JaxbUtil(PayNotifyResponse.class).fromXml(result, "utf-8");
            // 返回错误结果的处理
            String retCode = responseObj.getHeader().getResultCode();
            System.out.println("retCode是多少：" + retCode);
            if (false == Constant.RET_CODE_0000.equalsIgnoreCase(retCode)) {
                System.out.println("同步支付结果错误");
            } else {
                // 所有的通知结束之后，将数据库里面的isNotify变成0，即为不需要再次通知
                UserboundPaymentOrder paymentOrder = new UserboundPaymentOrder();
                paymentOrder.setIsNotify("0");
                paymentOrder.setOrderId(orderId);
                userboundPaymentOrderService.updateIsNotify(paymentOrder);

                // 当程序跑到这一步就真的完成了
                System.out.println("同步支付结果成功");

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
